package app16;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class SeekableByteChannelDemo1{
    public static void main(String[] args){
        ByteBuffer buffer=ByteBuffer.allocate(12);
        System.out.println(buffer.position());
        buffer.putInt(10);
        System.out.println(buffer.position());
        buffer.putLong(1234567890L);
        System.out.println(buffer.position());
        buffer.rewind();
        System.out.println(buffer.getInt());
        System.out.println(buffer.getLong());
        buffer.rewind();
        System.out.println(buffer.position());

        Path path=Paths.get("C:\\Users\\yangxiaopang\\Desktop\\py4e\\ex_02_03\\code3\\Origin.py");
        System.out.println("-----------------");
        try(SeekableByteChannel byteChannel=
                    Files.newByteChannel(path,
                            StandardOpenOption.CREATE,
                            StandardOpenOption.READ,
                            StandardOpenOption.WRITE);){
            System.out.println(byteChannel.position());
            byteChannel.write(buffer);
            System.out.println(byteChannel.position());


            //read file
            ByteBuffer buffer3=ByteBuffer.allocate(40);
            byteChannel.position(0);
            byteChannel.read(buffer3);
            buffer3.rewind();
            System.out.println("get int:"+buffer3.getInt());
            System.out.println("gey long:"+buffer3.getLong());
            System.out.println(buffer3.getChar());
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
